﻿Public Class frmQuanLyTapHoa

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Dim dshinhanh As ImageList = New ImageList()
        Dim dt As DataTable = TapHoaDAO.ThongTinTatCaSanPham()
        Dim i As Integer = 0
        Dim lv As DataRow
        For Each lv In dt.Rows
            Dim hinhanh As Image = Image.FromFile(lv("hh_HinhAnh").ToString())
            imgSanPham.Images.Add(hinhanh)
            lvlDanhSachSanPham.LargeImageList = imgSanPham
            Dim bk As ListViewItem = New ListViewItem(lv("hh_TenHang").ToString(), i)
            i = i + 1
            lvlDanhSachSanPham.Items.Add(bk)
        Next
        lvlDanhSachMua.Columns.Add("", 0)
        lvlDanhSachMua.Columns.Add("Tên sản phẩm", 100)
        lvlDanhSachMua.Columns.Add("Vị trí", 70)
        lvlDanhSachMua.Columns.Add("Số lượng", 70)
        lvlDanhSachMua.Columns.Add("Giá tiền", 100)

        'Dim th As TapHoa = New TapHoa()
        'th.m_intIDTaiKhoan = Me.Tag
        'If (TapHoaDAO.LayThongTinTaiKhoanID(th).Rows(0)(7).ToString() = 1) Then
        '    lbLoiChao.Text = "Xin chào chủ tiệm " & TapHoaDAO.LayThongTinTaiKhoanID(th).Rows(0)(3).ToString()
        'Else
        '    lbLoiChao.Text = "Xin chào nhân viên " & TapHoaDAO.LayThongTinTaiKhoanID(th).Rows(0)(3).ToString()
        'End If
    End Sub
    Dim TongGia As Integer
    Dim SoLuong As Integer
    Dim Gia As Integer
    Private Sub btnThem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnThem.Click
        If txtSoLuong.Text = "" Then
            MessageBox.Show("Chưa nhập số lượng sản phẩm cần mua !")
            Return
        End If
        Dim th As TapHoa = New TapHoa()
        Dim i As Integer
        While i < lvlDanhSachSanPham.Items.Count
            If (lvlDanhSachSanPham.Items(i).Selected = True) Then
                th.m_strTenHangHoa = lvlDanhSachSanPham.Items(i).Text.ToString()
                Dim dt As DataTable = TapHoaDAO.LayThongTinMotSanPham(th)
                Dim LaySoLuong As Integer = Integer.Parse(dt.Rows(0)("hh_SoLuong"))
                If LaySoLuong < Integer.Parse(txtSoLuong.Text) Then
                    MessageBox.Show("Trong kho không còn đủ số lượng cần mua")
                    txtSoLuong.Focus()
                    Return
                Else
                    Dim lv As DataRow
                    For Each lv In dt.Rows
                        Dim item As New ListViewItem()
                        th.m_intIDViTri = Integer.Parse(lv("hh_ViTri").ToString())
                        Dim vt As DataTable = TapHoaDAO.LayTenViTri(th)
                        item.SubItems.Add(lv("hh_TenHang").ToString())
                        For Each lt In vt.Rows
                            item.SubItems.Add(lt("vt_NoiVT").ToString())
                        Next
                        item.SubItems.Add(txtSoLuong.Text.ToString())
                        SoLuong = SoLuong + txtSoLuong.Text.ToString()

                        Gia = lv("hh_Gia").ToString() * txtSoLuong.Text.ToString()
                        item.SubItems.Add(Gia)
                        TongGia = TongGia + Gia
                        lvlDanhSachMua.Items.Add(item)
                        lbTongTien.Text = TongGia.ToString()
                        lbSoLuong.Text = SoLuong.ToString()
                    Next
                End If
                
            End If
            i = i + 1
        End While
    End Sub

    Private Sub btnXoa_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnXoa.Click
        Dim i As Integer
        While i < lvlDanhSachMua.Items.Count
            If (lvlDanhSachMua.Items(i).Selected) Then
                Dim SoLuongMua As Integer
                Dim GiaTien As Integer
                SoLuongMua = lvlDanhSachMua.Items(i).SubItems(3).Text.ToString()
                GiaTien = Integer.Parse(lvlDanhSachMua.Items(i).SubItems(4).Text.ToString())
                lbSoLuong.Text = lbSoLuong.Text - SoLuongMua
                TongGia = TongGia - GiaTien
                lbTongTien.Text = TongGia.ToString()
                lvlDanhSachMua.Items(i).Remove()
            End If
            i = i + 1
        End While
    End Sub

    Private Sub btnThanhToan_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnThanhToan.Click
        Dim th As TapHoa = New TapHoa()
        Dim mahoadon As Integer
        th.m_intTongTien = lbTongTien.Text
        th.m_dtNgayGio = lbNgayGio.Text.ToString()
        th.m_intIDNhanVien = 1
        Dim dt As DataTable = TapHoaDAO.LayIDHoaDon(th)
        mahoadon = dt.Rows(0)("ID").ToString()
        Dim IDSanPham As Integer
        Dim i As Integer = 0
        While i < lvlDanhSachMua.Items.Count
            th.m_intMaHoaDon = mahoadon
            th.m_strTenHangHoa = lvlDanhSachMua.Items(i).SubItems(1).Text.ToString()
            Dim ab As DataTable = TapHoaDAO.LayThongTinMotSanPham(th)
            IDSanPham = ab.Rows(0)("hh_ID").ToString()
            th.m_intIDSanPham = IDSanPham
            th.m_intSoLuong = Integer.Parse(lvlDanhSachMua.Items(i).SubItems(3).Text)
            th.m_intTongGia = Integer.Parse(lvlDanhSachMua.Items(i).SubItems(4).Text)
            Dim th1 As TapHoa = New TapHoa()
            th1.m_intSoLuong = Integer.Parse(lvlDanhSachMua.Items(i).SubItems(3).Text)
            th1.m_strTenHangHoa = lvlDanhSachMua.Items(i).SubItems(1).Text.ToString()
            TapHoaDAO.TruSoLuongHangHoa(th1)
            TapHoaDAO.ThemChiTietDonHang(th)
            i = i + 1
        End While
        lvlDanhSachMua.Items.Clear()
        lbSoLuong.Text = 0
        lbTongTien.Text = 0 & " VND"
        Dim frm As frmThanhToan = New frmThanhToan()
        frm.Tag = mahoadon
        frm.Show()
    End Sub

    Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
        lbNgayGio.Text = DateTime.Now
    End Sub

    Private Sub btnTimKiem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnTimKiem.Click
        Dim th As TapHoa = New TapHoa()
        th.m_strTenHangHoa = txtTimKiem.Text
        If (TapHoaDAO.TimKiemSanPhamTheoTen(th).Rows.Count > 0) Then
            lvlDanhSachSanPham.Items.Clear()
            Dim dshinhanh As ImageList = New ImageList()
            Dim dt As DataTable = TapHoaDAO.TimKiemSanPhamTheoTen(th)
            Dim i As Integer = 0
            Dim lv As DataRow
            For Each lv In dt.Rows
                Dim hinhanh As Image = Image.FromFile(lv("hh_HinhAnh").ToString())
                imgSanPham.Images.Add(hinhanh)
                lvlDanhSachSanPham.LargeImageList = imgSanPham
                Dim bk As ListViewItem = New ListViewItem(lv("hh_TenHang").ToString(), i)
                i = i + 1
                lvlDanhSachSanPham.Items.Add(bk)
            Next
        Else
            MessageBox.Show("Không tìm thấy sản phẩm " & txtTimKiem.Text)
        End If
    End Sub

    Private Sub btnTatCaSanPham_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnTatCaSanPham.Click
        lvlDanhSachSanPham.Items.Clear()
        Dim dshinhanh As ImageList = New ImageList()
        Dim dt As DataTable = TapHoaDAO.ThongTinTatCaSanPham()
        Dim i As Integer = 0
        Dim lv As DataRow
        For Each lv In dt.Rows
            Dim hinhanh As Image = Image.FromFile(lv("hh_HinhAnh").ToString())
            imgSanPham.Images.Add(hinhanh)
            lvlDanhSachSanPham.LargeImageList = imgSanPham
            Dim bk As ListViewItem = New ListViewItem(lv("hh_TenHang").ToString(), i)
            i = i + 1
            lvlDanhSachSanPham.Items.Add(bk)
        Next
    End Sub

    Private Sub btnTimKiemNangCao_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnTimKiemNangCao.Click
        Dim frm As frmTimKiemNangCao = New frmTimKiemNangCao()
        frm.Show()
    End Sub

    Private Sub lvlDanhSachSanPham_DoubleClick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles lvlDanhSachSanPham.DoubleClick
        Dim frm As frmThongTinSanPham = New frmThongTinSanPham()
        Dim i As Integer = 0
        While i < lvlDanhSachSanPham.Items.Count
            If (lvlDanhSachSanPham.Items(i).Selected = True) Then
                frm.Tag = lvlDanhSachMua
                lbTenSanPham.Text = lvlDanhSachSanPham.Items(i).Text.ToString()
                frm.Show()
            End If
            i = i + 1
        End While
    End Sub

    Private Sub XemKhoToolStripMenuItem_Click(sender As System.Object, e As System.EventArgs) Handles XemKhoToolStripMenuItem.Click

    End Sub
End Class